package ioperation;

import Book.Book;
import Book.BookShelf;

import java.util.Scanner;

public class DeleteOperation implements IOperation {
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("#########删除图书#########");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除的图书书名：");
        String name = scanner.nextLine();
        int currentSize = bookShelf.getUsedSize();
        int pos = -1;
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookShelf.getBooks(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }


        if (i == currentSize) {
            System.out.println("没有要删除的数据");
            return;
        } else {
            for (int j = pos; j < currentSize-1; j++) {//防止越界
                Book tmp = bookShelf.getBooks(j + 1);
                bookShelf.setBooks(tmp,j);
            }
        }
        System.out.println("删除成功！！！！");
        bookShelf.setBooks(null,currentSize-1);

        bookShelf.setUsedSize(currentSize-1);
    }
}

